package com.lecloud.api.connect;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import cn.edu.live.status.RespStatus;
import cn.edu.live.util.TimeUtil;

import com.alibaba.fastjson.JSONObject;
import com.lecloud.constant.Const;
import com.lecloud.util.LeCloudVerify;

/**
 * 创建直播房间
 * 
 * @author zhagz
 * 
 */
public class CreateLive {

	private static final String CreateLive_method = "lecloud.cloudlive.activity.create";

	private static final String CreateLive_ver = "4.1";

	/**
	 * 成功返回activityId，失败返回null
	 * 
	 * @param map
	 * @return
	 */
	public JSONObject createLiveRoom(Map<String, String> map) {
		// 获得sign
		map.put("method", CreateLive_method);
		map.put("timestamp", TimeUtil.currentTime() + "");
		map.put("userid", Const.userid + "");
		map.put("ver", CreateLive_ver);

		String sign = LeCloudVerify.getSign(Const.secretKey, map);

		map.put("sign", sign);

		try {
			Response resp = Jsoup.connect(Const.common_url).data(map)
					.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8").ignoreContentType(true)
					.timeout(20000).method(Method.POST).execute();
			System.out.println(resp.body());
			if (resp.statusCode() == RespStatus.SUCCESS) {
				JSONObject js = JSONObject.parseObject(resp.body());
				return js;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
